﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class QuestionDAL
    {
        public QuestionInfo GetByID(int listid)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listid)
            };
            try
            {
                dr = db.RunProc("T2R_Question_GetByID", param);
                if (dr.Read())
                {
                    QuestionInfo info = new QuestionInfo();
                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.Question = DataHelper.GetString(dr["Question"]);
                    info.QuestionType = DataHelper.GetString(dr["QuestionType"]);
                    info.Void = DataHelper.GetInt(dr["Void"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    return info;
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }

        public int Add(QuestionInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@Question",SqlDbType.VarChar,5000,info.Question),
                db.MakeInParam("@QuestionType",SqlDbType.VarChar,50,info.QuestionType),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID))
            };
            try
            {
                db.ExecNonQuery("T2R_Question_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public void Update(QuestionInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,info.ListID),
                db.MakeInParam("@Question",SqlDbType.VarChar,5000,info.Question),
                db.MakeInParam("@QuestionType",SqlDbType.VarChar,50,info.QuestionType),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID))
            };
            try
            {
                db.ExecNonQuery("T2R_Question_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public void Delete(int listid)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listid)
            };
            try
            {
                db.ExecNonQuery("T2R_Question_Delete", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public DataTable GetBySetID(string setID)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@QuestionSetID", SqlDbType.UniqueIdentifier, 16, new Guid(setID))
                };
                return db.GetDataSet("T2R_Question_GetBySetID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public DataTable GetQuestionsByUserGUID(string userguid)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(userguid))
                };
                return db.GetDataSet("T2R_Question_GetByUserGUID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public DataTable GetByUserGUID(string userguid)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(userguid))
                };
                return db.GetDataSet("T2R_QuestionSet_GetByUserGUID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public DataTable GetAll()
        {
            Database db = new Database();

            try
            {
                return db.GetDataSet("T2R_Question_GetAll").Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public int AddQuestionSet(QuestionSetInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@QuestionSetID",SqlDbType.UniqueIdentifier,16,new Guid(info.QuestionSetID)),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID)),
                db.MakeInParam("@QuestionID",SqlDbType.Int,4,info.QuestionID),
                db.MakeInParam("@OrderNum",SqlDbType.Int,4,info.OrderNum)
            };
            try
            {
                db.ExecNonQuery("T2R_QuestionSet_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public int UpdateQuestionSetOrderNum(int listID, int orderNum)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listID),
                db.MakeInParam("@OrderNum",SqlDbType.Int,4,orderNum)
            };
            try
            {
                db.ExecNonQuery("T2R_QuestionSet_UpdateOrderNum", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
    }
}
